<?php
/**
 * created by zzy
 * date: 2017/10/24 9:28
 */

namespace App\Eloquent\Ygt;
use App\Engine\Func;
use DB;
class ProcessField extends DbEloquent{

    protected $table    = 'ygt_process_field';

    //取字段分类列表,不同type值针对bjui等需要的地方
    public static function getFieldTypeList( $type=1 ){

        $lists              = config('process.process_field_type_list');
        $result             = Func::getIdTitleByType( $lists, $type );
        return $result;
    }
    //取工序字典的分类,不同type值针对bjui等需要的地方
    public static function getTypeList( $type=3 ){
        $lists              = config('process.field_type_list');
        $result             = Func::getIdTitleByType( $lists, $type );
        return $result;
    }
    //通过字段名取字段类型
    public static function getFieldTypeByFieldName( $fieldName, $companyId ){

        $where                  = ['field_name'=>$fieldName];
        $where['company_id']   = $companyId;
        $result                 = ProcessFieldCompany::getOneValue( $where, 'field_type' );
        return $result;
    }
    //设置厂的工序字典
    public static function setCompanyFieldList($companyId,$fromCompanyId=1)
    {
        if($fromCompanyId==1){
            $where['company_id']    = $companyId;
            $collection               = ProcessFieldCompany::getList( $where );
            $lists                    = $collection->toArray();
            $fieldIdArr               = [];
            foreach( $lists as $key=>$val )
            {
                $fieldIdArr[]         = $val['field_id'];
            }
            $result                  = true;
            $where                   = [];
            if( $fieldIdArr ){
                $where['id']        = ['notIn',$fieldIdArr];
            }
            $fieldList               = self::getList($where)->toArray();
            if( $fieldList ){
                $data               = [];
                foreach( $fieldList as $key=>$val)
                {
                    $arr                               = $val;
                    unset($arr['id']);
                    $arr['company_id']                = $companyId;
                    $arr['field_id']                  = $val['id'];
                    $arr['category_id']               = 0;
                    $arr['type']                        = 0;
                    $data[]                            = $arr;
                }
                $result             = DB::table('ygt_process_field_company')->insert($data);
            }
            return $result;
        }else{
            DB::table('ygt_process_field_company')->where('company_id',$companyId)->delete();
            $where['company_id']    = $fromCompanyId;
            $collection               = ProcessFieldCompany::getList( $where );
            $lists                    = $collection->toArray();
            $data                    = [];
            foreach( $lists as $key=>$val)
            {
                $arr                               = $val;
                unset($arr['id']);
                $arr['company_id']                = $companyId;
                $arr['category_id']               = 0;
                $arr['type']                      = 0;
                $data[]                            = $arr;
            }
            $result             = DB::table('ygt_process_field_company')->insert($data);
            return $result;
        }
    }
    //取厂的工序字典列表
    public static function getCompanyFieldList( $companyId, $where=[] ){

        self::setCompanyFieldList($companyId);
        $where['company_id']       = $companyId;
        $collection                 = ProcessFieldCompany::getList($where);
        $collection                 = $collection->sortBy('field_id')->sortBy('type');
        $lists                      = $collection->values()->toArray();
        $result                     = [];
        foreach( $lists as $key=>$val )
        {
            $arr                    = $val;
            $arr['id']              = $val['field_id'];
            $result[]               = $arr;
        }
        return $result;
    }
    //编辑厂的工序字典字段,若是$company_id为1是主表的默认的
    public static function editCompanyField( $companyId, $params )
    {
        if( !$companyId )
        {
            return false;
        }
        $id                 = isset($params['id']) ? $params['id'] : 0;
        $title              = $params['title'];
        $type               = $params['type'];
        $placeholder        = $params['placeholder'];
        $fieldType         = $params['field_type'];
        $textType          = $params['text_type'];
        $processFieldData = [
            'title'=>$title,'type'=>$type,'placeholder'=>$placeholder,
            'field_type'=>$fieldType,'text_type'=>$textType
        ];
        //$company_id为1时 可以添加即$id可为空
        if( $companyId == 1 )
        {
            //$id存在是编辑不存在是添加
            if( $id )
            {
                $where                  = ['title'=>$title,'id'=>['!=',$id]];
                $isExist               = self::getInfo( $where );
                if( $isExist )
                {
                    $result             = 'exist';
                } else {
                    //修改主表
                    $where              = ['id'=>$id];
                    $updateResult       = self::updateOneData( $where, $processFieldData );
                    //同时修改厂字段表
                    $where              = ['company_id'=>$companyId,'field_id'=>$id];
                    ProcessFieldCompany::updateOneData( $where, $processFieldData );
                    $result             = $updateResult ? true : false;
                }
            } else {
                $where                  = ['title'=>$title];
                $isExist                = self::getInfo( $where );
                if( $isExist ){
                    $result            = 'exist';
                } else {
                    $nowTime           = $_SERVER['REQUEST_TIME'];
                    $fieldName         = 'a'.$nowTime;
                    $processFieldData['field_name']   = $fieldName;
                    $insert_id          = self::insertOneData( $processFieldData,'id' );
                    $result             = false;
                    if( $insert_id )
                    {
                        $where          = ['id'=>$insert_id];
                        $fieldName     = 'field_name_'.$insert_id;
                        $data           = [ 'field_name'=>$fieldName ];
                        $result         = self::updateOneData( $where, $data );
                        $sql            = "ALTER TABLE `ygt_order_process` ADD COLUMN `".$fieldName."` VARCHAR(100) DEFAULT '' NOT NULL";
                        $sql2           = "ALTER TABLE `ygt_order_process_pre` ADD COLUMN `".$fieldName."` VARCHAR(100) DEFAULT '' NOT NULL";
                        $sql3           = "ALTER TABLE `ygt_customer_order_process` ADD COLUMN `".$fieldName."` VARCHAR(100) DEFAULT '' NOT NULL";
                        $sql4           = "ALTER TABLE `ygt_customer_order_process_pre` ADD COLUMN `".$fieldName."` VARCHAR(100) DEFAULT '' NOT NULL";
                        DB::statement($sql);
                        DB::statement($sql2);
                        DB::statement($sql3);
                        DB::statement($sql4);
                        $id             = $insert_id;
                    }
                }
            }
        } else {
            //厂字段编辑 只能修改不能添加
            $result                     = false;
            if( $id )
            {
                $where                  = ['company_id'=>$companyId,'title'=>$title,'field_id'=>['!=',$id]];
                $isExist               = ProcessFieldCompany::getInfo( $where );
                if( $isExist ){
                    $result             = 'exist';
                } else {
                    $where              = ['company_id'=>$companyId,'field_id'=>$id];
                    $updateResult      = ProcessFieldCompany::updateOneData( $where, $processFieldData );
                    $result             = $updateResult ? true : false;
                }
            }
        }
        //return $result;
        $resultData     = [];
        if( $result === true )
        {
            if($companyId == 1){
                $where          = ['id'=>$id];
                $resultData     = self::getInfo($where)->toArray();
            } else {
                $where          = ['company_id'=>$companyId,'field_id'=>$id];
                $resultData     = ProcessFieldCompany::getInfo($where)->toArray();
                $resultData['id']   = $id;
            }
        }
        $resultArr      = ['status'=>$result,'data'=>$resultData];
        return $resultArr;
    }
}
